'use strict'

const path = require('path');
const { TaiSpider, ItemLoader } = require('tai-spider');
const { saveJsonFile } = require('../utils/file-helper');
const { formatDate } = require('../utils/str-helper');

const keywords = [
    'space surveillance',
    'space awareness',
    'space policy',
    'space strategy',
    'space threat',
    'space program',
    'satellite launch',
    'missile launch',
    'rocket launch',
    'satellite image',
    'satellite enter service',
    'countspace',
    'space debris',
    'ballistic missile',
    'missile defense',
    'missile warning',
    'anti-ballistic missile defense',
    'orbital maneuver',
    'approach',
    'rendezvous',
    'malfunction',
    'reentry',
    'decay',
    'nuclear explosion',
    'break up',
];

class TassSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'tass';
        this.debug = true;
        this.start_urls = ['https://tass.com/'];
        // () => keywords.map((keyword) => `https://tass.com/?searchStr=${encodeURIComponent(keyword)}&sort=date`);
        // this.start_urls = [
        //     'https://tass.com/science/1362325',
        // ];
    }

    *parse(response) {
        let headers = {
            "accept": "application/json, text/plain, */*",
            "content-type": "application/json;charset=UTF-8",
            "origin": "https://tass.com",
            "referer": "https://tass.com/search?searchStr=space&sort=date",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
            "cookie": "_ym_uid=16680793221016980521; _ym_d=1696130506; __js_p_=242,1800,0,0,0; __jhash_=456; __jua_=Mozilla%2F5.0%20%28X11%3B%20Linux%20x86_64%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F110.0.0.0%20Safari%2F537.36; __hash_=e2ccc91e1ca7ee2cc761527230133b20; __lhash_=90950afe4f5b18f88775ce55cb6a05a1; _ym_isad=1",
        };
        // for (let keyword of keywords) {
            let keyword = keywords[0];
            let json = {
                "type": [],
                "sections": [],
                "searchStr": keyword,
                "sort": "date",
                "range": null,
                "from": 0,
                "size": 50
            }
            yield response.from_request({
                link: 'https://tass.com/userApi/search',
                method: 'POST',
                json,
                headers,
                http2: true,
                cb: this.parseList,
            });
        // }
    }

    *parseList(response) {
        response.saveBody("error.html")
        for (let ele of response.getJSON()) {
            yield {
                'title': ele.title,
                'link': ele.link,
                'pubdate': new Date(ele.date * 1000),
            };
            yield response.follow(ele.link, this.parseArticle);
        }
    }

    *parseArticle(response, spider) {
        // const { store } = spider.options;
        let data = response.extract();
        let node = response.css('.news-header__date-date').get(0).child(0);
        if (node) {
            let date = formatDate(node.attr('time') * 1000)
            data.timestamp = date.valueOf();
            data.date = date.format();
        }
        // yield saveJsonFile(path.join(store.path, data.title + '.json'), data)

        yield Object.assign({
            index_name: 'tai-article',
        }, data);
        // const loader = new ItemLoader(response, require('../model/article'));
        // yield loader.load_item();
    }
}

module.exports = TassSpider;